{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [1] Python3 基本数据类型 - 六个标准的数据类型\n",
    "#\n",
    "\n",
    "''' \n",
    "Python 中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。\n",
    "'''\n",
    "counter = 100       # 整型变量\n",
    "miles = 1000.0      # 浮点型变量\n",
    "name = \"runoob\"     # 字符串\n",
    "is_true = True      # 布尔变量\n",
    "\n",
    "# print('[1] Python3 基本数据类型')\n",
    "\n",
    "'''\n",
    "多个变量赋值\n",
    "'''\n",
    "x = y = z = 1\n",
    "a, b, c = 1, 2, \"runoob\"\n",
    "\n",
    "''' \n",
    "Python3 中有六个标准的数据类型：\n",
    "    Number       - 数字\n",
    "    String       - 字符串\n",
    "    List         - 列表\n",
    "    Tuple        - 元组\n",
    "    Set          - 集合\n",
    "    Dictionary   - 字典\n",
    "\n",
    "Python3 的六个标准数据类型中：\n",
    "不可变数据  Number、String、Tuple\n",
    "可变数据    List、Dictionary、Set\n",
    "'''\n",
    "\n",
    "\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [2] Python3 基本数据类型 - Number\n",
    "#\n",
    "''' \n",
    "Python3 支持 int、float、bool、complex.\n",
    "在Python 3里, 只有一种整数类型 int, 表示为长整型\n",
    "'''\n",
    "a, b, c, d = 20, 5.5, True, 4+3j\n",
    "print(type(a), type(b), type(c), type(d))\n",
    "\n",
    "'''\n",
    "isinstance 和 type 的区别在于：\n",
    "type()不会认为子类是一种父类类型。\n",
    "isinstance()会认为子类是一种父类类型。\n",
    "'''\n",
    "a = 111\n",
    "print(isinstance(a, int))\n",
    "\n",
    "\n",
    "class A:\n",
    "    pass\n",
    "\n",
    "\n",
    "class B(A):\n",
    "    pass\n",
    "\n",
    "\n",
    "print(isinstance(A(), A))\n",
    "print(type(A()) == A)\n",
    "\n",
    "print(isinstance(B(), A))\n",
    "print(type(B()) == A)\n",
    "\n",
    "\n",
    "# 使用del语句删除一些对象引用\n",
    "var = 1\n",
    "print(var)\n",
    "del var\n",
    "# print(var) # NameError: name 'var' is not defined\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [3] Python3 基本数据类型 - String\n",
    "#\n",
    "''' \n",
    "Python中的字符串用单引号 ' 或双引号 \" 括起来，同时使用反斜杠 \\ 转义特殊字符。\n",
    "字符串的截取的语法格式如下：\n",
    "    变量[头下标:尾下标]\n",
    "\n",
    "索引值以 0 为开始值，-1 为从末尾的开始位置\n",
    "加号 + 是字符串的连接符， 星号 * 表示复制当前字符串，与之结合的数字为复制的次数。\n",
    "\n",
    "与 C 字符串不同的是, Python 字符串不能被改变。向一个索引位置赋值，比如 word[0] = 'm' 会导致错误。\n",
    "Python 没有单独的字符类型, 一个字符就是长度为1的字符串。\n",
    "'''\n",
    "str = 'Runoob'\n",
    "\n",
    "print(str)              # 输出字符串\n",
    "print(str[0:-1])        # 输出第一个到倒数第二个的所有字符\n",
    "print(str[0])           # 输出字符串第一个字符\n",
    "print(str[2:5])         # 输出从第三个开始到第五个的字符\n",
    "print(str[2:])          # 输出从第三个开始的后的所有字符\n",
    "print(str * 2)          # 输出字符串两次，也可以写成 print (2 * str)\n",
    "print(str + \"TEST\")     # 连接字符串\n",
    "\n",
    "''' \n",
    "使用反斜杠 \\ 转义特殊字符，\n",
    "如果你不想让反斜杠发生转义，可以在字符串前面添加一个 r 表示原始字符串\n",
    "'''\n",
    "print('Ru\\noob')    # 换行\n",
    "print(r'Ru\\noob')   # 输出 \\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [4] Python 基本数据类型 - List => []\n",
    "# \n",
    "# List（列表） 是 Python 中使用最频繁的数据类型。\n",
    "# 列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同，它支持数字，字符串甚至可以包含列表（所谓嵌套）。\n",
    "# 列表是写在方括号[] 之间、用逗号分隔开的元素列表。\n",
    "# 和字符串一样，列表同样可以被索引和截取，列表被截取后返回一个包含所需元素的新列表。\n",
    "# \n",
    "# 列表截取的语法格式如下：\n",
    "# 变量[头下标:尾下标]\n",
    "# \n",
    "# 索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "# \n",
    "'''\n",
    "t = [1,  2,  3,  4,  5]\n",
    "     |   |   |   |   |\n",
    "     0   1   2   3   4\n",
    "    -5  -4  -3  -2  -1\n",
    "\n",
    "'''\n",
    "# 加号 + 是列表连接运算符，星号 * 是重复操作。如下实例：\n",
    "# \n",
    "list = ['abcd', 786, 2.23, 'runoob', 70.2]\n",
    "tinylist = [123, 'runoob']\n",
    "\n",
    "print(list)             # 输出完整列表\n",
    "print(list[0])          # 输出列表第一个元素\n",
    "print(list[1:3])        # 从第二个开始输出到第三个元素\n",
    "print(list[2:])         # 输出从第三个元素开始的所有元素\n",
    "print(tinylist * 2)     # 输出两次列表\n",
    "print(list + tinylist)  # 连接列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [5] Python 基本数据类型 - Tuple => ()\n",
    "#\n",
    "# 元组（tuple）与列表类似，不同之处在于元组的元素不能修改。\n",
    "# 元组写在小括号() 里，元素之间用逗号隔开。\n",
    "# 元组中的元素类型也可以不相同：\n",
    "# \n",
    "\n",
    "tuple = ('abcd', 786, 2.23, 'runoob', 70.2)\n",
    "tinytuple = (123, 'runoob')\n",
    "\n",
    "print(tuple)                # 输出完整元组\n",
    "print(tuple[0])             # 输出元组的第一个元素\n",
    "print(tuple[1:3])           # 输出从第二个元素开始到第三个元素\n",
    "print(tuple[2:])            # 输出从第三个元素开始的所有元素\n",
    "print(tinytuple * 2)        # 输出两次元组\n",
    "print(tuple + tinytuple)    # 连接元组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [6] Python 基本数据类型 - Set => {...}\n",
    "#\n",
    "# 集合（set）是由一个或数个形态各异的大小整体组成的，构成集合的事物或对象称作元素或是成员。\n",
    "# 基本功能是进行成员关系测试和删除重复元素。\n",
    "# 可以使用大括号 {} 或者 set() 函数创建集合，\n",
    "# 注意：创建一个空集合必须用 set() 而不是 {}，因为 {} 是用来创建一个空字典。\n",
    "# \n",
    "# 创建格式：\n",
    "# parame = {value01, value02, ...}\n",
    "# 或者\n",
    "# set(value)\n",
    "# \n",
    "sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu'}\n",
    "print(sites)   # 输出集合，重复的元素被自动去掉\n",
    "\n",
    "# 成员测试\n",
    "if 'Runoob' in sites:\n",
    "    print('Runoob 在集合中')\n",
    "else:\n",
    "    print('Runoob 不在集合中')\n",
    "\n",
    "# set可以进行集合运算\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "\n",
    "print(a)\n",
    "print(a - b)     # a 和 b 的差集\n",
    "print(a | b)     # a 和 b 的并集\n",
    "print(a & b)     # a 和 b 的交集\n",
    "print(a ^ b)     # a 和 b 中不同时存在的元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [7] Python 基本数据类型 - Dictionary（字典）\n",
    "#\n",
    "# 字典（dictionary）是 Python中另一个非常有用的内置数据类型。\n",
    "# 列表是有序的对象集合，字典是无序的对象集合。两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。\n",
    "# 字典是一种映射类型，字典用 {} 标识，它是一个无序的 键(key): 值(value) 的集合。\n",
    "# 键(key)必须使用不可变类型。\n",
    "# 在同一个字典中，键(key)必须是唯一的。\n",
    "#\n",
    "dict1 = {}  # 创建一个空字典， 相当于 dict1 = dict()\n",
    "dict1['one'] = \"1 - 菜鸟教程\"\n",
    "dict1[2] = \"2 - 菜鸟工具\"\n",
    "\n",
    "tinydict = {'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'}\n",
    "\n",
    "print(dict1['one'])         # 输出键为 'one' 的值\n",
    "print(dict1[2])             # 输出键为 2 的值\n",
    "print(tinydict)             # 输出完整的字典\n",
    "print(tinydict.keys())      # 输出所有键\n",
    "print(tinydict.values())    # 输出所有值\n",
    "\n",
    "# 构造函数 dict() 可以直接从键值对序列中构建字典如下：\n",
    "x = dict([('Runoob', 1), ('Google', 2), ('Taobao', 3)])\n",
    "x = dict()\n",
    "print(x)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "60d0b561bcf179bd0cd76361af4313e00281c3c5daca1903769e6cc3dd82b37c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
